package com.kingdee.pda.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kingdee.pda.R;
import com.kingdee.pda.util.FastClickUtil;

/**
 * Created by zhanlg
 * DATE 2018/12/18
 * TIME 19:37
 */

public class RemindDialog extends BaseAlertDialog<RemindDialog> implements View.OnClickListener {

    public interface OnConfirmClickListener {
        void onConfirmClick();
    }


    private Context mContext;
    private View view;
    private ImageView mIvStatusShow;
    private TextView mTvTitle;
    private View mLineView;
    private TextView mTvContent;
    private Button mBtnCancel;
    private View mViewLine;
    private LinearLayout mLayoutButton;
    private String mTitle;
    private String mContent;
    private OnConfirmClickListener mListener;
    private boolean isShowStatusView = false;
    private boolean isShowTitle = true;
    private boolean isShowCancel = false;
    private int mStatusViewResId = 0;
    private int contentGravity = Gravity.CENTER;

    public void setContentGravity(int contentGravity) {
        this.contentGravity = contentGravity;
    }

    public RemindDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public RemindDialog(Context context, int contentGravity) {
        super(context);
        this.mContext = context;
        this.contentGravity = contentGravity;
    }

    public RemindDialog(Context context, String remindContent, OnConfirmClickListener listener) {
        super(context);
        this.mContext = context;
        this.mContent = remindContent;
        this.mListener = listener;
    }

    @Override
    public View onCreateView() {
        setCanceledOnTouchOutside(false);
        view = LayoutInflater.from(mContext).inflate(R.layout.dialog_remind, null);
        mLlContainer.addView(view);
        initView();
        return mLlContainer;
    }

    private void initView() {
        mIvStatusShow = view.findViewById(R.id.iv_status_show);
        mTvTitle = view.findViewById(R.id.tv_title);
        mLineView = view.findViewById(R.id.line_view);
        mTvContent = view.findViewById(R.id.tv_content);
        mBtnCancel = view.findViewById(R.id.btn_cancel);
        mViewLine = view.findViewById(R.id.view_line);
        mLayoutButton = view.findViewById(R.id.layout_button);
        mBtnCancel.setOnClickListener(this);
        view.findViewById(R.id.btn_confirm).setOnClickListener(this);

        if (!TextUtils.isEmpty(mTitle)) {
            mTvTitle.setText(mTitle);
        }
        mTvContent.setText(mContent);
        mTvContent.setGravity(contentGravity);
        if (isShowStatusView) {
            mIvStatusShow.setVisibility(View.VISIBLE);
            mIvStatusShow.setImageResource(mStatusViewResId);
        }
        if (!isShowTitle) {
            mTvTitle.setVisibility(View.GONE);
            mLineView.setVisibility(View.GONE);
        }
        if (isShowCancel) {
            mBtnCancel.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        if (FastClickUtil.isFastClickByTime(500)) {
            return;
        }
        switch (v.getId()) {
            case R.id.btn_confirm:
                if (mListener != null) {
                    mListener.onConfirmClick();
                }
                this.cancel();
                break;
            case R.id.btn_cancel:
                this.cancel();
                break;
        }
    }

    public void setContent(String content) {
        this.mContent = content;
        if (mTvContent != null) {
            mTvContent.setText(content);
        }
    }

    public void setNoTitle() {
        this.isShowTitle = false;
    }

    public void setTitle(String title) {
        this.mTitle = title;
        if (mTvTitle != null) {
            mTvTitle.setText(mTitle);
        }
    }

    public void setShowCancelButton() {
        this.isShowCancel = true;
    }

    public void setShowStatusView(int resId) {
        this.isShowStatusView = true;
        this.mStatusViewResId = resId;
    }

    public void setListener(OnConfirmClickListener mListener) {
        this.mListener = mListener;
    }

    /**
     * show方法后调用
     */
    public void setAllButtonVisible(boolean isShow) {
        if (mLayoutButton != null) {
            if (isShow) {
                mViewLine.setVisibility(View.VISIBLE);
                mLayoutButton.setVisibility(View.VISIBLE);
            } else {
                mViewLine.setVisibility(View.GONE);
                mLayoutButton.setVisibility(View.GONE);
            }
        }
    }


    @Override
    public void onBackPressed() {
//        super.onBackPressed();
    }
}
